<?php

namespace PRokkoMVC {
   /***********************************************************
    * 
    ***********************************************************/
   abstract class PRokkoController {
      protected $wpdb;
      protected $appName;
      protected $appRoot;
      protected $model;
   
      /***********************************************************
       * 
       ***********************************************************/
      public function __construct($pWpdb, $pAppName, $pAppRoot) {
         $this->wpdb = $pWpdb;
         $this->appName = $pAppName;
         $this->appRoot = $pAppRoot;
         $this->model = array();
      }
      
      
      /***********************************************************
       * 
       ***********************************************************/
      final protected function loadModel($pNs, $pModel) {
         $model = "\\{$pNs}\\{$pModel}";
         $this->model[$pModel] = new $model($this->wpdb);
      }
      
      
      /***********************************************************
       *
       ***********************************************************/
      final protected function redirect($pPath = "") {
      
         $uri = explode("?", $_SERVER["REQUEST_URI"]);
         $path = $uri[0]."?page=".$this->appName."&".$pPath;
         
         header("Location: {$path}");
      }
      
      /***********************************************************
       * @param $pTmpl string a Mustache.php template
       * @param $pData array the data to be parsed in the Mustache.php template
       ***********************************************************/
      final protected function render($pTmpl, $pData) {
         echo $this->getView($pTmpl, $pData);
      }
      
      
      /***********************************************************
       * @param $pTmpl string a Mustache.php template
       * @param $pData array the data to be parsed in the Mustache.php template
       ***********************************************************/
      final protected function getView($pTmpl, $pData) {
         $mustache;
      
         //
         // Load mustache if it's not already loaded
         //
         try {
            $mustache = new \Mustache();
         } catch (MustacheException $e) {
            require_once __DIR__."/../php/Mustache.php";
            $mustache = new \Mustache();
         }
          
         $tmpl = file_get_contents($this->appRoot."/../views/".$pTmpl);
         
         //
         // A little pathing dance that allows for resources to be referenced from the plugin's root directory
         //
         $linkerPath = explode("plugins\\", $this->appRoot);
         $p = "../wp-content/plugins/".$linkerPath[1]."/../../";
         $pData["PROKKOMVC"] = array(
               "APP_ROOT" => $p,
               "APP_NAME" => $this->appName
               );

         return $mustache->render($tmpl, $pData);
      }
      
      
      /***********************************************************
       *
       ***********************************************************/
      abstract public function index();
   }
}
